/**
 * 分页扩展模块
 * ------------------------------
 * 使用例如：
 * var xmPage = layui.xmPage({
                formElem:'#UserForm',
                limit:limit,
                count:count,
                curr:currPage,
                layout:['prev','page','next','skip','count','limit'],
                prev:'<i class="fa fa-arrow-left"></i>',
                next:'<i class="fa fa-arrow-right"></i>',
   });
 */
layui.define(['laypage', 'jquery'], function (exports) {
    var $ = layui.jquery,
        laypage = layui.laypage;
    var xmPage = function () {
        this.pageConfig = {
            elem: 'pageShow',
            count: 1,
            limit: 10,//每页条数
            formElem: 'search_form',
            limits: [10, 20, 30, 40, 50],
            curr: 1,
            layout: ['prev', 'page', 'next'],
            jump: function (obj, first) {
                var currPage = obj.curr;
                var form = page.pageConfig.formElem;

                var limit = obj.limit;
                $("#limit").val(limit);
                $("#page").val(currPage);
                if (limit != page.currentLimit) {
                    $("#page").val(1);
                }
                if (!first) {
                    console.log('1')
                    $(form).submit();
                }

            }
        }
    }
    xmPage.prototype.set = function (options) {
        var _this = this;
        $.extend(true, _this.pageConfig, options);
        return _this;
    }

    xmPage.prototype.render = function () {
        var _that = this;
        //根据数据初始化form信息
        var form = page.pageConfig.formElem;
        if ($(form).find('#limit').length == 0) {
            $(form).append('<input type="hidden" name="limit" id="limit" value="' + page.pageConfig.limit + '" />');
        }
        if ($(form).find('#page').length == 0) {
            $(form).append('<input type="hidden" name="page" id="page" value="' + page.pageConfig.curr + '" />');
        }
        _that.currentLimit = page.pageConfig.limit;
        laypage.render(_that.pageConfig)
    }

    var page = new xmPage();
    exports('xmPage', function (options) {
        return page.set(options);
    });
})